/** 
 * @author (Gruppe G) 
 * @version (04/18/2012)
 */

package ModelLayer;
import java.util.ArrayList;

public class LendCollection
{
    private static LendCollection instance;
    private ArrayList<Lend> LendList;


    /**
     * Constructor for objects of class LendCollection
     */
    private LendCollection()
    {
        LendList = new ArrayList<Lend>();
    }
    
    /**
     * Creates an instance of the LendCollection.
     */
    public static LendCollection getInstance()
    {
        if (instance == null)
        {
            instance = new LendCollection();
        }
        return instance;
    }
    
    /**
     * Creating a lend entry in the array.
     */
    public void addLend(Lend newLend)
    {
        LendList.add(newLend);
    }
    
    /**
     * Finds a lend via the persons phonenumer in array.
     * 
     * @param personsPhone the phonenumber to be used in search
     */
    public Lend findLend(String personPhone)
    {
        Lend lendObj = null;
        int index = 0;
        boolean found = false;
        while(index < LendList.size() && !found)
        {
            lendObj = LendList.get(index);
            Person personObj = lendObj.getPerson();
            if(personObj.getPhone().equals(personPhone))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        return lendObj;
    }
    
    /**
     * Finding all lends belonging to a person in array.
     * 
     * @param personPhone the phonenumber to be used in search
     */
    public ArrayList<Lend> findAllLends(String personPhone)
    {
        ArrayList<Lend> foundLends = new ArrayList<Lend>();
        for (Lend lendObj : LendList)
        {
            Person personObj = lendObj.getPerson();
            if(personObj.getPhone().equals(personPhone))
            {
                foundLends.add(lendObj);
            }
        } 
        return foundLends;
    }
    
    /**
     * Returning a lend via the persons phonenumber and the movie title.
     * 
     * @param personPhone the phonenumber to be used in returning the lend
     * @param title the movie title to be used in returning the lend
     */
    public Lend ReturnLend(String personPhone, String title)
    {
        Lend lendObj = null;
        int index = 0;
        boolean found = false;
        while(index < LendList.size() && !found)
        {
            lendObj = LendList.get(index);
            Person personObj = lendObj.getPerson();
            DVD DVDObj = lendObj.getDVD();
            
            if(personObj.getPhone().equals(personPhone) && DVDObj.getTitle().equals(title))
            {
                found = true;
                lendObj.setStatus("Returned");
            }
            else
            {
                index++;
            }
        }
        return lendObj;
    }
}